/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.server.modules.sys.web.intention;

import com.server.common.config.Global;
import com.server.common.persistence.Page;
import com.server.common.utils.StringUtils;
import com.server.common.web.BaseController;
import com.server.modules.sys.entity.intention.TCustomerIntention;
import com.server.modules.sys.entity.material.MaterialType;
import com.server.modules.sys.service.intention.TCustomerIntentionService;
import com.server.modules.sys.service.loan.LoanService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 潜在客户信息记录Controller
 * @author 金小敏
 * @version 2017-04-01
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/intention/tCustomerIntention")
public class TCustomerIntentionController extends BaseController {

	@Autowired
	private TCustomerIntentionService tCustomerIntentionService;
	@Autowired
	private LoanService loanService;
	
	@ModelAttribute
	public TCustomerIntention get(@RequestParam(required=false) String id) {
		TCustomerIntention entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = tCustomerIntentionService.get(id);
		}
		if (entity == null){
			entity = new TCustomerIntention();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:intention:tCustomerIntention:view")
	@RequestMapping(value = {"list", ""})
	public String list(TCustomerIntention tCustomerIntention, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TCustomerIntention> page = tCustomerIntentionService.findPage(new Page<TCustomerIntention>(request, response), tCustomerIntention); 
		model.addAttribute("page", page);
		model.addAttribute("tCustomerIntention", tCustomerIntention);
		return "modules/sys/intention/tCustomerIntentionList";
	}

	@RequiresPermissions("sys:intention:tCustomerIntention:view")
	@RequestMapping(value = "form")
	public String form(TCustomerIntention tCustomerIntention, Model model) {
		List<MaterialType> materialTypes = loanService.getMaterialTypeNames();
		model.addAttribute("materialTyps",materialTypes);
		model.addAttribute("tCustomerIntention", tCustomerIntention);
		return "modules/sys/intention/tCustomerIntentionForm";
	}
//	@RequiresPermissions("sys:intention:tCustomerIntention:view")
//	@RequestMapping(value = "form")
//	public ModelAndView getMaterialTypeNames(){
//		ModelAndView mx = new ModelAndView();
//		List<MaterialType> materialTypes = 	loanService.getMaterialTypeNames();
//		mx.addObject("materialTypes",materialTypes);
//		mx.setViewName("/modules/sys/intention/tCustomerIntentionForm");
//		return mx;
//	}

	@RequiresPermissions("sys:intention:tCustomerIntention:edit")
	@RequestMapping(value = "save")
	public String save(TCustomerIntention tCustomerIntention, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tCustomerIntention)){
			return form(tCustomerIntention, model);
		}
		tCustomerIntentionService.save(tCustomerIntention);
		addMessage(redirectAttributes, "保存潜在客户信息记录成功");
		return "redirect:"+Global.getAdminPath()+"/sys/intention/tCustomerIntention/?repage";
	}
	
	@RequiresPermissions("sys:intention:tCustomerIntention:edit")
	@RequestMapping(value = "delete")
	public String delete(TCustomerIntention tCustomerIntention, RedirectAttributes redirectAttributes) {
		tCustomerIntentionService.delete(tCustomerIntention);
		addMessage(redirectAttributes, "删除潜在客户信息记录成功");
		return "redirect:"+ Global.getAdminPath()+"/sys/intention/tCustomerIntention/?repage";
	}

}